package com.wealth.lottery.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.wealth.lottery.entity.LotteryPrize;

import iaas.repository.BaseRepository;
/**
 * LotteryPrizeDao
 * @author lihang 2016-06-27
 *
 */
@Repository
public interface LotteryPrizeDao extends BaseRepository<LotteryPrize, Long>,JpaSpecificationExecutor<LotteryPrize>{

	/**
	 * 查询可以兑换的奖品
	 */
	public String sql= "select * from lottery_prize a "
                   +" WHERE lottery_id = ?1 and corp_id=?2 and daysum > (select count(*) from lottery_record c "+
                   	"WHERE corp_id=?2 and lottery_id=?1 AND c.prize_id = a.id and c.status=1 "
                   	+ "And create_time >= (select date_format(now(), '%Y-%m-%d 00:00:00') oneday_starttime) and create_time "
                   	+ " <= (select date_format(now(), '%Y-%m-%d 23:59:59') oneday_endtime) "
                   	+ ") AND (a.maxsum-a.total)>0";
	//@Query(value=sql,nativeQuery = true)
	//List<LotteryPrize> findByLotteryactivityIdAndCorpId(Long activityid,Long corpid);
	/**查询是否有奖品被抽中*/
	@Query(value="select *from lottery_prize where lottery_id=?1 and corp_id=?3 and  find_in_set(?2,lucknum)",nativeQuery = true)
	List<LotteryPrize> findByLotteryactivityIdAndLucknumAndCorpId(Long activityid,int lucknum,Long corpid);
	/**查询活动下所有的商品*/
	List<LotteryPrize> findByLotteryactivityIdAndCorpId(Long activityid,Long corpid);
}
